<?php

namespace app\models\order;

use app\models\system\SetExpressCompany;
use app\models\user\UserAddress;
use Yii;

/**
 * This is the model class for table "order_logistics".
 *
 * @property int $id
 * @property int $order_id 订单id
 * @property int $order_item_id 拆单用order_item_id
 * @property int $express_company_id 物流公司, 对应set_express_company id
 * @property string $express_sn 快递单号
 * @property int $user_address_id 收货地址, 对应user_address id
 * @property int $status 0 待发货 1 已发货 -1 已退货
 * @property string $create_time
 * @property string $update_time
 */
class OrderLogistics extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'order_logistics';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['order_id', 'order_item_id', 'express_company_id', 'user_address_id', 'status'], 'integer'],
            [['create_time', 'update_time'], 'safe'],
            [['express_sn'], 'safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'order_id' => 'Order ID',
            'order_item_id' => 'Order Item ID',
            'express_company_id' => 'Express Company ID',
            'express_sn' => 'Express Sn',
            'user_address_id' => 'User Address ID',
            'status' => 'Status',
            'create_time' => 'Create Time',
            'update_time' => 'Update Time',
        ];
    }

    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            $time = date('Y-m-d H:i:s');
            $this->update_time = $time;
            if ($insert) {
                $this->create_time = $time;
            }
            return true;
        }
        return false;
    }

    public function getCompany()
    {
        // $express_company_id 物流公司, 对应set_express_company id

        return $this->hasOne(SetExpressCompany::class, ['id' => 'express_company_id']);
    }

    public function getAddress()
    {
        // $user_address_id 收货地址, 对应user_address id

        return $this->hasOne(UserAddress::class, ['id' => 'user_address_id']);
    }
}
